﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DefeatEpidemic.Common;
using DefeatEpidemic.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace DefeatEpidemic.API
{
    [Route("api/[controller]/[action]")]
    [GlobalActionFilter]
    public class TestController : Controller
    {
        private readonly DefeatEpidemicContext _context;

        public TestController(DefeatEpidemicContext context)
        {
            _context = context;
        }

        [HttpGet]
        public async Task<Response> DelTestData(string open_id="")
        {
            Response resp = new Response() { code = 1 };
            try
            {
                if (!string.IsNullOrEmpty(open_id))
                {
                    var oauth = _context.Wxoauths.Where(u => u.open_id == open_id).FirstOrDefault();
                    await _context.Entry(oauth).Reference(d => d.member).LoadAsync();
                    var member = oauth.member;
                    _context.Remove(oauth);
                    if (member != null)
                    {
                        var applications = _context.Applications.Where(u => u.member.id == member.id);
                        if (applications.Count() > 0)
                        {
                            foreach (var application in applications)
                                _context.Remove(application);
                        }
                        _context.Remove(member);
                    }
                    int ret = await _context.SaveChangesAsync();
                    if (ret > 0)
                    {
                        resp.code = 1;
                        resp.message = "success";
                    }
                }
                else
                {
                    var oauths = _context.Wxoauths;
                    foreach(var oauth in oauths)
                    {
                        await _context.Entry(oauth).Reference(d => d.member).LoadAsync();
                        var member = oauth.member;                      
                        _context.Remove(oauth);
                        if (member!=null)
                        {
                            var applications = _context.Applications.Where(u => u.member.id == member.id);
                            if (applications.Count() > 0)
                            {
                                foreach (var application in applications)
                                    _context.Remove(application);
                            }
                            _context.Remove(member);
                        }
                        int ret = await _context.SaveChangesAsync();
                        if (ret > 0)
                        {
                            resp.code = 1;
                            resp.message = "success";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message},{ex.StackTrace}";
            }
            return resp;
        }
    }
}
